
Imports DevComponents.DotNetBar
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports quanlydulich.Helpers
	''' <summary>
	'''  manage functionalities corresponding with specified loai nguoi dung
	''' </summary>
	Public Partial Class frmQuanLyChucNangNhomNguoiDung
		Inherits Office2007Form
		' Using this variable to get which ma loai nguoi dung is specified
		Private SpecifiedMaLoaiNguoiDung As Integer = -1

		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub frmQuanLyChucNangNhomNguoiDung_Load(sender As Object, e As EventArgs)
			LoadLoaiNguoiDung()
			LoadChucNang()
		End Sub

		Private Sub btnDong_Click(sender As Object, e As EventArgs)
			Me.Close()
		End Sub

		''' <summary>
		''' Event is raised when user check on an item
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
		Private Sub lstvNhomNguoiDung_ItemCheck(sender As Object, e As ItemCheckEventArgs)
			If e.NewValue = CheckState.Checked Then
				For i As Integer = 0 To lstvNhomNguoiDung.Items.Count - 1
					If i <> e.Index Then
						lstvNhomNguoiDung.Items(i).Checked = False
					Else
						SpecifiedMaLoaiNguoiDung = CInt(lstvNhomNguoiDung.Items(e.Index).Tag)
						ResetStateOfChucNang()
						LoadChucNangBelongToNguoiDung(SpecifiedMaLoaiNguoiDung)
					End If
				Next
			End If
		End Sub

		Private Sub btnLuu_Click(sender As Object, e As EventArgs)
			' Only executed when there's at least 1 loai nguoi dung is chosen  
			If SpecifiedMaLoaiNguoiDung <> -1 Then
				' Get list of chose chuc nang
				Dim lstChoseChucNang = New List(Of Integer)()
				For i As Integer = 0 To lstvChucNangDuocSuDung.Items.Count - 1
					If lstvChucNangDuocSuDung.Items(i).Checked Then
						lstChoseChucNang.Add(CInt(lstvChucNangDuocSuDung.Items(i).Tag))
					End If
				Next
				If lstChoseChucNang.Count > 0 Then
					' Before insert new list of chuc nang for loai nguoi dung, we remove list of chuc nang belong to loai nguoi dung that have been associated before
					If DeleteChucNangBelongToNguoiDung(SpecifiedMaLoaiNguoiDung) Then
						Dim successful = False
						For i As Integer = 0 To lstChoseChucNang.Count - 1
                        Dim item As LOAINGUOIDUNG_CHUCNANGDTO = New LOAINGUOIDUNG_CHUCNANGDTO()
                        item.IDLoaiNguoiDung = SpecifiedMaLoaiNguoiDung
                        item.IDChucNang = lstChoseChucNang(i)
                        Dim objBUS As LOAINGUOIDUNG_CHUCNANGBUS = New LOAINGUOIDUNG_CHUCNANGBUS(GlobalVars.g_strConnectionString)
                        successful = objBUS.Insert(item)
                    Next
						If successful Then
							MessageBoxEx.Show("Lưu dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
						Else
							MessageBoxEx.Show("Có lỗi trong quá trình thực thi", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
						End If
					Else
						MessageBoxEx.Show("Có lỗi trong quá trình thực thi", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
					End If
				End If
			End If
		End Sub

		''' <summary>
		''' Reset check state of list all of chuc nang loaded in list view
		''' </summary>
		Public Sub ResetStateOfChucNang()
			For i As Integer = 0 To lstvChucNangDuocSuDung.Items.Count - 1
				lstvChucNangDuocSuDung.Items(i).Checked = False
			Next
		End Sub

		''' <summary>
		''' Load list of chuc nang belong to specified loai nguoi dung
		''' </summary>
		Public Sub LoadChucNangBelongToNguoiDung(maloainguoidung As Integer)
        Try
            Dim objBUS As LOAINGUOIDUNG_CHUCNANGBUS = New LOAINGUOIDUNG_CHUCNANGBUS(GlobalVars.g_strConnectionString)
            Dim objDS As DataSet = objBUS.SelectByIDLoaiNguoiDung(maloainguoidung)
            If objDS.Tables(0).Rows.Count > 0 Then
                ' Loop for obtained list of chuc nang
                For i As Integer = 0 To objDS.Tables(0).Rows.Count - 1
                    ' Loop for obtained list all of chuc nang, if which item have ma chuc nang equal specified chuc nang
                    ' => Set check state is checked
                    For j As Integer = 0 To lstvChucNangDuocSuDung.Items.Count - 1
                        If CInt(lstvChucNangDuocSuDung.Items(j).Tag) = objDS.Tables(0).Rows(i)("IDChucNang").ToString() Then
                            lstvChucNangDuocSuDung.Items(j).Checked = True
                        End If
                    Next
                Next
            End If
        Catch ex As Exception
            Throw New Exception(ex.Message, ex.InnerException)
        End Try
		End Sub

		''' <summary>
		''' Load list all of loai nguoi dung exist in DB
		''' </summary>
		Public Sub LoadLoaiNguoiDung()
        Try
            Dim objBUS As LOAINGUOIDUNGBUS = New LOAINGUOIDUNGBUS(GlobalVars.g_strConnectionString)
            Dim objDS As DataSet = objBUS.SelectAll()
            If objDS.Tables(0).Rows.Count > 0 Then
                Dim objListViewItem As ListViewItem
                lstvNhomNguoiDung.Items.Clear()
                For i As Integer = 0 To objDS.Tables(0).Rows.Count - 1
                    objListViewItem = New ListViewItem()
                    objListViewItem.Tag = objDS.Tables(0).Rows(i)("ID").ToString()
                    objListViewItem.Text = objDS.Tables(0).Rows(i)("TenLoaiNguoiDung").ToString()
                    objListViewItem.ImageIndex = 0
                    lstvNhomNguoiDung.Items.Add(objListViewItem)
                Next
            End If
        Catch e As System.Exception
            Throw New Exception(e.Message, e.InnerException)
        End Try
		End Sub

		''' <summary>
		''' Load list all of chuc nang exist in DB
		''' </summary>
		Public Sub LoadChucNang()
        Try
            Dim objBUS As CHUCNANGBUS = New CHUCNANGBUS(GlobalVars.g_strConnectionString)
            Dim objDS As DataSet = objBUS.SelectAll()
            If objDS.Tables(0).Rows.Count > 0 Then
                Dim objListViewItem As ListViewItem
                lstvChucNangDuocSuDung.Items.Clear()
                For i As Integer = 0 To objDS.Tables(0).Rows.Count - 1
                    objListViewItem = New ListViewItem()
                    objListViewItem.Tag = objDS.Tables(0).Rows(i)("ID").ToString()
                    objListViewItem.SubItems.Add(objDS.Tables(0).Rows(i)("TenChucNang").ToString())
                    lstvChucNangDuocSuDung.Items.Add(objListViewItem)
                Next
            End If
        Catch e As System.Exception
            Throw New Exception(e.Message, e.InnerException)
        End Try
		End Sub

		''' <summary>
		''' Remove list of chuc nang belong to loai nguoi dung
		''' </summary>
		''' <param name="maloainguoidung"></param>
		''' <returns></returns>
		Private Function DeleteChucNangBelongToNguoiDung(maloainguoidung As Integer) As Boolean
        Try
            Dim objBUS As LOAINGUOIDUNG_CHUCNANGBUS = New LOAINGUOIDUNG_CHUCNANGBUS(GlobalVars.g_strConnectionString)
            Dim objDS As DataSet = objBUS.SelectByIDLoaiNguoiDung(maloainguoidung)
            If (objDS.Tables(0).Rows.Count > 0) Then
                For i As Integer = 0 To objDS.Tables(0).Rows.Count - 1
                    objBUS = New LOAINGUOIDUNG_CHUCNANGBUS(GlobalVars.g_strConnectionString)
                    objBUS.Delete(Integer.Parse(objDS.Tables(0).Rows(i)("ID").ToString()))
                Next
            End If
            Return True
        Catch
            Return False
        End Try
		End Function
	End Class